<?php

class dataAnalysis {
   
   var $dbobj;
   var $table;
   var $metric_name;
   var $calc_date;
   var $dataset_id;
   var $key_cols = array(); // user submits a key value array with key_name => key_value
   var $metric_value;
   var $isTemporal = TRUE;
   var $stardate = '1900-01-01';
   var $enddate = '1999-12-31';
   var $value_type = 'single'; // single, multiple (stored as array in $metric_value)
   var $multi_key = ''; // provides a key to use for multiple values - if no key provided, just returns an array of values
   // not yet used
   var $query = '';
   var $column_maps = array(); // key => value is local_name => database column name
   
   function calculate() {
      
      if (!is_object($this->dbobj)) {
         return FALSE;
      }
      if (!method_exists($this->dbobj,'performQuery')) {
         return FALSE;
      }
      $this->assembleQuery();
      if ($this->query == '') {
         return FALSE;
      }
      $this->dbobj->querystring = $this->query;
      $this->dbobj->performQuery();
      if ($this->dbobj->numrows > 0) {
         switch ($this->value_type) {
            case 'single':
            $this->metric_value = $this->dbobj->getRecordValue(1,$this->metric_name);
            break;

            case 'multiple':
            $this->metric_value = extract_keyvalue($this->dbobj->queryrecords, $this->metric_name);
            break;
         }
      } else {
         $this->metric_value = FALSE;
      }
   }
   
   function assembleQuery () {
      // this class performs no query, it must be subclassed to do anything
      $this->query = '';
      $colstring = $this->getColumns();
      if ($colstring == '') {
         return;
      }
      $this->query = " SELECT $colstring FROM \"$this->table\" ";
      $this->query .= $this->assembleWhere();
   }
   
   function assembleWhere() {
      $ws = '';
      $wdel = 'WHERE';
      foreach ($this->key_cols as $key => $value) {
         if (is_numeric($value)) {
            $vd = "";
         } else {
            $vd = "'";
         }
         $ws .= " $wdel \"$key\" = $vd$value$vd ";
         $wdel = 'AND';
      }
      return $ws;
   }
   
   function assembleGroup() {
      // should be subclassed
      $gs = '';
      return $gs;
   }
   
}


class dataMetric_monthly_stat_pct extends dataAnalysis {
   // calculates the summary stat for the month, then gets the value for the the desired return frequency of this stat
   var $month = 1;
   var $pct = 0.5;
   var $metric_name = 'q';
   var $isTemporal = TRUE;
   var $value_type = 'single';
   var $yeartype = 'calendar'; // options: calendar - water
   var $stat = 'avg'; // could be  avg, median, min, max
   var $column_maps = array('q'=>'Q', 'yearcol'=>'year', 'mocol'=>'month');
   
   function assembleQuery () {
      // this class performs no query, it must be subclassed to do anything
      $qcol = $this->column_maps['q'];
      $yearcol = $this->column_maps['yearcol'];
      $this->query = "  select r_quantile(array_accum(q), $this->pct) as \"$this->metric_name\" ";
      $this->query .= " from (";
      $this->query .= "    select " . $this->stat . "(\"$qcol\") as q ";
      $this->query .= "    from \"$this->table\" ";
      $this->key_cols['month'] = $this->month;
      $this->query .= $this->assembleWhere();
      $this->query .= "    group by \"$yearcol\" ";
      $this->query .= "    order by \"$yearcol\" ";
      $this->query .= " ) as foo ";
      
   }
   
}

?>